package com.retail.bbq.cola.controller;

import com.retail.bbq.cola.annotation.CheckHeader;
import com.retail.bbq.cola.annotation.EncryptResponse;
import com.retail.bbq.cola.service.WechatCustomerService;
import com.retail.bbq.cola.vo.ResultVO;
import com.retail.bbq.cola.vo.request.SaveUserInfoRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user-login")
@Slf4j
public class UserLoginController {

    @Resource
    private WechatCustomerService wechatCustomerService;

    @EncryptResponse
    //@DecryptResponse
    @GetMapping("/api/sp-portal/store/{buCode}/wechat/loginNoCache/{code}")
    @CheckHeader(requiredHeaders = {"Authorization"})
    public ResultVO<?> userLoginNoCache(HttpServletRequest request,
                                        @PathVariable String buCode,
                                        @PathVariable String code,
                                        @RequestParam("source") String source,
                                        @RequestParam("oneid") String oneid,
                                        @RequestParam("encryptMobile") String encryptMobile){
        return wechatCustomerService.userLoginNoCache(request, buCode, code, source, oneid, encryptMobile);
    }

    @RequestMapping("/userInfoSaveOrUpdate")
    public ResultVO<?> userInfoSaveOrUpdate(@RequestBody SaveUserInfoRequest request){
        log.info("用户信息保存或更新,请求参数:{}", request);
        return wechatCustomerService.userInfoSaveOrUpdate(request);
    }

    @RequestMapping("/getUserInfo")
    public ResultVO<?> getUserInfo(@RequestBody SaveUserInfoRequest request){
        log.info("获取用户信息,请求参数:{}", request);
        return ResultVO.succ(wechatCustomerService.getUserInfoOrDefault(request.getOneid(), request.getLawsonId()));
    }
}
